/*
 * @(#)NavigatorTag.java 
 * 
 * Project: CUPSecureBM
 * 
 * Modify Information:
 * =============================================================================
 * Author           Date              Description 
 * hitrust          2005-12-7         first release
 * 
 * Copyright Notice:
 * =============================================================================
 * Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 * Science & Technology Building, No. 9 South Street, Zhong Guan Cun, Haidian
 * District, Beijing ,100081, China All rights reserved.
 * 
 * This software is the confidential and proprietary information of Beijing
 * HiTRUST Technology Co., Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with HiTRUST.
 * 
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.util.page.taglib.page;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.hitrust.gs.util.page.StringUtil;

/**
 * 显示导航条,可利用此改变每页的记录数,上下翻页和跳页
 * 使用方法:<page:navigator type='TEXT' QueryForm='XXXXform'/>
 * @author Starboy
 * @version 2.0
 */
public class NavigatorTag extends TagSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 导航条的类型(BUTTON/TEXT)(按钮型/文字链接型)
     * 
     */
    private String type = "BUTTON"; //选择导航条类型默认"BUTTON"(BUTTON/TEXT)
    /**
     * 用于执行查询的Form
     */
    private String queryForm ="pager";
    
    public void setType(String newType) {
        type = newType;
    }
    
    public void setQueryForm(String newQueryForm) {
        queryForm = newQueryForm;
    }

    public int doStartTag() throws JspException {
        try {
            String bar = getNavigatorBar(type);
            pageContext.getOut().write(bar);
            return SKIP_BODY;
        }
        catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
    }

    public int doEndTag() throws JspException {

        return EVAL_PAGE;

    }

    /**
     * 根据指定类型获得导航条预先设计的导航条
     * @param type 导航条类型(BUTTON/TEXT)
     * @return 返回导航条的HTML代码,若指定类型不存在,返回""
     */
    private String getNavigatorBar(String type) {
        String bar = "";
        String pageNo   = ((Integer) pageContext.getAttribute("pageNo")).toString();
        String pages    = ((Integer) pageContext.getAttribute("pages")).toString();
        String total    = ((Integer) pageContext.getAttribute("total")).toString();
        String pageSize = ((Integer) pageContext.getAttribute("pageSize")).toString();
        String nextDisabled = "";
        String prevDisabled = "";
        if (Integer.parseInt(pageNo) >= Integer.parseInt(pages))
            nextDisabled = "disabled";
        if (Integer.parseInt(pageNo) <= 1)
            prevDisabled = "disabled";

        //---------------------按钮型的导航条-----------------------//
        if (type.equalsIgnoreCase("BUTTON")) {
            String pageSizeInput = "<input type='text' size='2' value='" + pageSize + "' "
                                   + "onChange=\"javascript:document."+queryForm+".choice.value='current';"
                                   + "document."+queryForm+".pageSize.value=this.value;document."+queryForm+".submit();\">";
            String firstButton = "<input  class='kuang' type='button' value='首  页' " + prevDisabled + " "
                                 +
                    "onClick=\"javascript:document."+queryForm+".choice.value='first';document."+queryForm+".submit();\">";
            String prevButton = "<input class='kuang'  type='button' value='上一页' " + prevDisabled + " "
                                +
                    "onClick=\"javascript:document."+queryForm+".choice.value='prev';document."+queryForm+".submit();\">";
            String nextButton = "<input  class='kuang' type='button' value='下一页' " + nextDisabled + " "
                                +
                    "onClick=\"javascript:document."+queryForm+".choice.value='next';document."+queryForm+".submit();\">";
            String lastButton = "<input  class='kuang' type='button' value='最后一页' " + nextDisabled + " "
                                +
                    "onClick=\"javascript:document."+queryForm+".choice.value='last';document."+queryForm+".submit();\">";
            String pageNoInput = "<input type='text' size='3' value='" + pageNo + "' "
                                 + "onChange=\"javascript:document."+queryForm+".pageNo.value=this.value\">";

            bar = "每页pageSize条记录 | \n"
                  + "共pages页/total条记录 | \n"
                  + "first \n prev \n next \n last \n | 第pageNo页\n"
                  +
                    " <input type='submit' value='go' onClick=\"javascript:document."+queryForm+".choice.value='current';document."+queryForm+".submit();return false\">\n";

            bar = StringUtil.replace(bar, "pageSize", pageSizeInput);
            bar = StringUtil.replace(bar, "pages", pages);
            bar = StringUtil.replace(bar, "total", total);
            bar = StringUtil.replace(bar, "first", firstButton);
            bar = StringUtil.replace(bar, "prev", prevButton);
            bar = StringUtil.replace(bar, "next", nextButton);
            bar = StringUtil.replace(bar, "last", lastButton);
            bar = StringUtil.replace(bar, "pageNo", pageNoInput);
        } /////end of if(button)

        //-------------------------文字型----------------------------//
        if (type.equalsIgnoreCase("TEXT")) {
        	String ty=(String)pageContext.getRequest().getAttribute("type");
            StringBuffer tPageSizeInputStrBuff = new StringBuffer();
            
            tPageSizeInputStrBuff.append(" " + pageSize + " ");
            tPageSizeInputStrBuff.append("<input type='hidden' size='2' value='"                                                      );
			//old
			//tPageSizeInputStrBuff.append("<input type='text' size='2' value='"                                                      );
            tPageSizeInputStrBuff.append(pageSize                                                                                   );
            tPageSizeInputStrBuff.append("' maxLength='2' "                                                                         );
            tPageSizeInputStrBuff.append("onChange=\"document."+queryForm+".choice.value='current';"                                        );
            tPageSizeInputStrBuff.append("document."+queryForm+".pageSize.value=this.value;"                                                     );
            tPageSizeInputStrBuff.append("if(this.value==null || this.value=='') document."+queryForm+".pageSize.value = 10 ;"                   );
            tPageSizeInputStrBuff.append("else{"                                                                                    );
            tPageSizeInputStrBuff.append(" for(var i=0; i<this.value.length; i++){"                                                 );
            tPageSizeInputStrBuff.append("  var ch = this.value.charAt(i);"                                                         );
            tPageSizeInputStrBuff.append("   if(ch == '0' && i==0){ "                                                               );
            tPageSizeInputStrBuff.append("       alert('每页显示笔数必须为正整数');"                                                  );
            tPageSizeInputStrBuff.append("       return false;"                                                                     );
            tPageSizeInputStrBuff.append("   }"                                                                                     );
            tPageSizeInputStrBuff.append("  if(ch < '0' || ch > '9'){ "                                                             );
            tPageSizeInputStrBuff.append("      alert('每页显示笔数必须为正整数');"                                                   );
            tPageSizeInputStrBuff.append("      return false;"                                                                      );
            tPageSizeInputStrBuff.append("  }"                                                                                      );
            tPageSizeInputStrBuff.append("}"                                                                                        );
            tPageSizeInputStrBuff.append("if(this.value > 10) {alert('每页最大显示笔数不能大于10');return false;}else{document."+queryForm+".pageSize.value=this.value;}}\">");            
            String pageSizeInput = new String(tPageSizeInputStrBuff);
                                   
            String firstText = "首  页";
            String prevText = "上一页";
            String nextText = "下一页";
            String lastText = "最后一页";
            if (prevDisabled.equalsIgnoreCase("")) {
            	if(ty!=null&&!"".equals(ty)){
            	firstText = "<a href=\"javascript:document."+queryForm+".choice.value='first';";
            	firstText+="document."+queryForm+".type.value='";
            	firstText+=ty;
            	firstText+="';";
            	firstText+="document."+queryForm+".submit();\"> 首  页 </a>";
                prevText  = "<a href=\"javascript:document."+queryForm+".choice.value='prev' ;";
                prevText+="document."+queryForm+".type.value='";
                prevText+=ty;
                prevText+="';";
                prevText+="document."+queryForm+".submit();\"> 上一页  </a>";
            	}else{
                firstText = "<a href=\"javascript:document."+queryForm+".choice.value='first';document."+queryForm+".submit();\"> 首  页 </a>";
                prevText  = "<a href=\"javascript:document."+queryForm+".choice.value='prev' ;document."+queryForm+".submit();\"> 上一页  </a>";
            	}
            }
            if (nextDisabled.equalsIgnoreCase("")) {
             	if(ty!=null&&!"".equals(ty)){
             	nextText = "<a href=\"javascript:document."+queryForm+".choice.value='next';";
             	nextText+="document."+queryForm+".type.value='";
             	nextText+=ty;
             	nextText+="';";
             	nextText+="document."+queryForm+".submit();\"> 下一页  </a>";
             	lastText  = "<a href=\"javascript:document."+queryForm+".choice.value='last' ;";
             	lastText+="document."+queryForm+".type.value='";
             	lastText+=ty;
             	lastText+="';";
             	lastText+="document."+queryForm+".submit();\"> 最后一页</a>";
             	}else{
                nextText  = "<a href=\"javascript:document."+queryForm+".choice.value='next';document."+queryForm+".submit();\"> 下一页  </a>";
                lastText  = "<a href=\"javascript:document."+queryForm+".choice.value='last';document."+queryForm+".submit();\"> 最后一页</a>";
             	}
             }

            StringBuffer tPageNoInputStrBuff = new StringBuffer();
            tPageNoInputStrBuff.append("<input type='text' size='3' size='2' value='"                            );
            tPageNoInputStrBuff.append(pageNo                                                                    );
            tPageNoInputStrBuff.append("' "                                                                      );
            tPageNoInputStrBuff.append("onChange=\"javascript:document."+queryForm+".pageNo.value=this.value;"                );
            tPageNoInputStrBuff.append("if(this.value == null || this.value == '') document."+queryForm+".pageNo.value='1';"  );
            tPageNoInputStrBuff.append("else{"                                                                   );
            tPageNoInputStrBuff.append("   for(var i=0; i<this.value.length;i++){"                               );
            tPageNoInputStrBuff.append("       var ch = this.value.charAt(i);"                                   );
            tPageNoInputStrBuff.append("       if( ch =='0' && i==0){"                                           );
            tPageNoInputStrBuff.append("           alert('页数必须为正整数');"                                    );
            tPageNoInputStrBuff.append("           return false;"                                                );
            tPageNoInputStrBuff.append("       }"                                                                );
            tPageNoInputStrBuff.append("       if( ch <'0' || ch > '9'){"                                        );
            tPageNoInputStrBuff.append("           alert('页数必须为正整数');"                                    );
            tPageNoInputStrBuff.append("           return false;"                                                );
            tPageNoInputStrBuff.append("       }"                                                                );
            tPageNoInputStrBuff.append("   }"                                                                    );
            tPageNoInputStrBuff.append("}\">"                                                                    );
            String pageNoInput = new String(tPageNoInputStrBuff);
        	if(ty!=null&&!"".equals(ty)){
        	bar = "每页pageSize条记录 | \n"
                     + "共pages页/total条记录 | \n"
                     + "first \n prev \n next \n last \n | 第pageNo页\n"
                     +
                    " <input type='button' value='go' onClick=\"javascript:document."+queryForm+".choice.value='current';" +
                    " document."+queryForm+".type.value='"  +
                    ty
                    +"';"+
                    " document."+queryForm+".submit();\">\n";
         	}else{
            bar = "每页pageSize条记录 | \n"
                  + "共pages页/total条记录 | \n"
                  + "first \n prev \n next \n last \n | 第pageNo页\n"
                  +
                    " <input type='button' value='go' onClick=\"javascript:document."+queryForm+".choice.value='current'; document."+queryForm+".submit();\">\n";
         	}
            bar = StringUtil.replace(bar, "pageSize", pageSizeInput);
            bar = StringUtil.replace(bar, "pages", pages);
            bar = StringUtil.replace(bar, "total", total);
            bar = StringUtil.replace(bar, "first", firstText);
            bar = StringUtil.replace(bar, "prev", prevText);
            bar = StringUtil.replace(bar, "next", nextText);
            bar = StringUtil.replace(bar, "last", lastText);
            bar = StringUtil.replace(bar, "pageNo", pageNoInput);
        } /////end of if(text)

        //---------------------按钮型的导航条-----------------------//
        if (type.equalsIgnoreCase("SIMPLEBUTTON")) {
            String pageSizeInput = "<input class='navbar' type='text' size='1' value='" + pageSize +
                                   "' "
                                   + "onChange=\"javascript:this.form.choice.value='current';"
                                   + "document."+queryForm+".pageSize.value=this.value;document."+queryForm+".submit();\">";
            String prevButton = "<input class='kuang' type='button' value='上一页' " + prevDisabled +
                                " "
                                +
                    "onClick=\"javascript:document."+queryForm+".choice.value='prev';document."+queryForm+".submit();\">";
            String nextButton = "<input class='kuang' type='button' value='下一页' " + nextDisabled +
                                " "
                                +
                    "onClick=\"javascript:document."+queryForm+".choice.value='next';document."+queryForm+".submit();\">";
            String pageNoInput = "<input class='navbar' type='text' size='1' value='" + pageNo +
                                 "' "
                                 + "onChange=\"javascript:this.form.pageNo.value=this.value\">";

            bar = "每页pageSize条记录 | \n"
                  + "共pages页/total条记录 | \n"
                  + "\n prev \n next \n | 第pageNo页\n"
                  + " <input class='navbar' type='submit' value='go' onClick=\"javascript:document."+queryForm+".choice.value='current'; document."+queryForm+".submit();\">\n";

            bar = StringUtil.replace(bar, "pageSize", pageSizeInput);
            bar = StringUtil.replace(bar, "pages", pages);
            bar = StringUtil.replace(bar, "total", total);
            bar = StringUtil.replace(bar, "prev", prevButton);
            bar = StringUtil.replace(bar, "next", nextButton);
            bar = StringUtil.replace(bar, "pageNo", pageNoInput);
        } /////end of if(button)

        //-------------------------文字型----------------------------//
        if (type.equalsIgnoreCase("SIMPLETEXT")) {
            String pageSizeInput = "<input type='text' size='1' value='" + pageSize + "' "
                                   + "onChange=\"javascript:document."+queryForm+".choice.value='current';"
                                   + "document."+queryForm+".pageSize.value=this.value;document."+queryForm+".submit();\">";
            String firstText = "首  页";
            String prevText = "上一页";
            String nextText = "下一页";
            String lastText = "最后一页";
            if (prevDisabled.equalsIgnoreCase("")) {
                firstText = "<a href=\"javascript:document.pager.choice.value='first';document."+queryForm+".submit();\"> 首  页 </a>";
                prevText = "<a href=\"javascript:document.pager.choice.value='prev';document."+queryForm+".submit();\">  上一页 </a>";
            }
            if (nextDisabled.equalsIgnoreCase("")) {
                nextText = "<a href=\"javascript:document."+queryForm+".choice.value='next';document."+queryForm+".submit();\"> 下一页 </a>";
                lastText = "<a href=\"javascript:document."+queryForm+".choice.value='last';document."+queryForm+".submit();\">  最后一页 </a>";
            }
            String pageNoInput = "<input type='text' size='1' value='" + pageNo + "' onChange=\"javascript:document."+queryForm+".pageNo.value=this.value\">";

            bar = "每页pageSize条记录 | \n"
                  + "共pages页/total条记录 | \n"
                  + "prev \n next \n | 第pageNo页\n"
                  + " <input type='submit' value='go' onClick=\"javascript:document."+queryForm+".choice.value='current'; document."+queryForm+".submit();\">\n";

            bar = StringUtil.replace(bar, "pageSize", pageSizeInput);
            bar = StringUtil.replace(bar, "pages", pages);
            bar = StringUtil.replace(bar, "total", total);
            bar = StringUtil.replace(bar, "first", firstText);
            bar = StringUtil.replace(bar, "prev" , prevText);
            bar = StringUtil.replace(bar, "next" , nextText);
            bar = StringUtil.replace(bar, "last" , lastText);
            bar = StringUtil.replace(bar, "pageNo", pageNoInput);
        } /////end of if(text)
        return bar;
    }
}